package model

import (
	"github.com/gogf/gf/database/gdb"
)

func Paginate(model *gdb.Model, page, limit int) (*PageResult, error) {
	total, err := model.Count()
	if err != nil {
		return nil, err
	}
	if total <= 0 {
		return nil, nil
	}
	list, err := model.Page(page, limit).All()
	if err != nil {
		return nil, err
	}
	return &PageResult{
		List:     list,
		Total:    total,
		Page:     page,
		PageSize: limit,
	}, nil
}
